<script src="../../../resources/gc.js"></script>
<body>
<p>Test that workers stay reachable via message ports.
Should print "DONE" when done.</p>
<div id=result></div>
<script>
function log(message)
{
    document.getElementById("result").innerHTML += message + "<br>";
}

if (window.testRunner) {
    testRunner.dumpAsText();
    testRunner.waitUntilDone();
}

// Test that workers stay alive even though they are only reachable via message ports.
var worker = new Worker("../resources/worker-messageport.js");
var channel = new MessageChannel();
worker.postMessage("port", [channel.port1]);
worker = 0;

channel.port2.postMessage("ping");
channel.port2.onmessage = function(evt) {
    // Other side is running, now force a GC, wait a bit, and send a new message to make sure it arrives.
    gc();
    setTimeout(testReachable, 100);
}

function testReachable()
{
    channel.port2.onmessage = function(evt) {
        log("PASS: Worker is reachable.");
        done();
    }
    channel.port2.postMessage("ping");
}

function done()
{
    log("DONE");
    if (window.testRunner)
        testRunner.notifyDone();
}

</script>
</body>
</html>

